package com.custom.adz.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.MotionEvent;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.ImageView;

/**
 * Created by Administrator on 2017/5/24.
 * 圆形取色器
 */

public class ColorPickerView extends FrameLayout {
    //Debug props
    private boolean mDrawDebug = false;
    private Paint mDebugPaint = null;
    private Point mLastSelectedColorPoint = null;

    private int mLastSelectedColor;

    //Views
    private ImageView mImgWheel;
    private ImageView mImgThumb;

    //Drawable references
    @Nullable
    private Drawable mWheelDrawable;
    private Drawable mThumbDrawable;

    //Path of the color range
    @NonNull
    private Path mThumbWheelPath;

    //Center
    private float mCenterX = 0;
    private float mCenterY = 0;
    private float mRadius = 0;
    private float mRadiusOffset = 0;

    public ColorPickerView(Context context) {
        super(context);
        init();
    }
    private void init() {
        //register first measture
        registerMeasure();
    }
    private void registerMeasure() {
        //获取全局的视图树观察者，注册回调响应布局改变事件
        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (Build.VERSION.SDK_INT < 16) {
                    //移除先前的回调
                    getViewTreeObserver().removeGlobalOnLayoutListener(this);
                } else {
                    //移除先前的回调
                    getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }
                onFirstLayout();
            }
        });
    }
    private void onFirstLayout() {
        //First layout, lets grab the size and generate the path for
        mThumbWheelPath = generateThumbWheelPath(840, 840);
    }
    private Path generateThumbWheelPath(int measuredWidth, int measuredHeight) {
        //By default we just make a circle. default scaletype of an imageview is fitCenter
        //Lets calculate the square size of the imageview's content
        int side = Math.min(measuredHeight, measuredWidth);
        //Lets create the path
        Path output = new Path();

        mRadius = getRadius(side);
        mCenterX = (float) measuredWidth / 2;
        mCenterY = (float) measuredHeight / 2;

        //add the offset of the imageview wheel in this viewgroup
        output.addCircle(getCenterXInParent(), getCenterYInParent(), mRadius, Path.Direction.CW);
        return output;
    }
    public float getRadius(float side) {
        //Offset the entire circle
        //Radius is the smallest side - the offset to center the circle in the center of the color circle.
        float radius = ((side) - 72) / 2;
        return radius;
    }
    private float getCenterXInParent() {
        return mCenterX +48;
    }

    private float getCenterYInParent() {
        return mCenterY + 48;
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
       // if (mDrawDebug && mThumbWheelPath != null) {
            checkDebugPaint();
            //Draw the path to see while debugging
            canvas.drawPath(mThumbWheelPath, mDebugPaint);
       // }
    }
    private void checkDebugPaint() {
        //Allocations during a draw phase are a big no-no. But will happen only once, and it's for debugging anyway.
        if (mDebugPaint == null) {
            mDebugPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mDebugPaint.setStrokeWidth(5f);
            mDebugPaint.setStyle(Paint.Style.STROKE);
            mDebugPaint.setColor(getResources().getColor(android.R.color.holo_red_dark));
        }
    }
}
